/*
 *The code is written by NaLi, All rights reserved.
 */
package com.nali.dal.statement.query;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.nali.dal.expression.PropertyExpression;
import com.nali.dal.expression.query.Criteria;
import com.nali.dal.statement.function.DataHandler;
import com.nali.dal.statement.function.Function;

/**
 * @author gavin Created on 2011-4-7
 */
public class SimpleColumn implements ColumnUnitConfig {
	static class SimpleColumnUnit implements ColumnUnit {
		private String name;
		private DataHandler<?> handler;
		private Function function;
		public SimpleColumnUnit(String name, DataHandler<?> handler, Function function) {
			super();
			this.name = name;
			this.handler = handler;
			this.function = function;
		}

		public String getName() {
			return name;
		}

		public Object getColumnValue(Map<String, Object> datas) {
			List<Object> argList = function.getArgs(datas);
			return handler.handle(argList);
		}
	}

	private List<String> columns;
	private SimpleColumnUnit columnUnit;
	
	public SimpleColumn(List<String> columns, String name, Function function, DataHandler<?> handler) {
		this.columns = columns;
		columnUnit=new SimpleColumnUnit(name, handler, function);
	}

	@Override
	public ColumnUnit getColumnUnit() {
		return this.columnUnit;
	}
	
	public List<String> getColumns() {
		return this.columns;
	}

	@Override
	public List<PropertyExpression<Criteria>> getColumnCriterias() {
		List<PropertyExpression<Criteria>> rlt=new ArrayList<PropertyExpression<Criteria>>(columns.size());
		for (String column : columns) {
			rlt.add(new PropertyExpression<Criteria>(column, Criteria.select));
		}
		return rlt;
	}
}
